package com.oceansoft.mobile.biz.admin.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.oceansoft.mobile.common.util.StringUtils;
import com.oceansoft.mobile.weixin.util.WeixinUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oceansoft.mobile.biz.admin.entity.Merchant;
import com.oceansoft.mobile.biz.admin.service.IMerchantService;
import com.oceansoft.mobile.common.base.entity.Pager;
import com.oceansoft.mobile.common.base.entity.Query;
import com.oceansoft.mobile.common.base.entity.Result;
import com.oceansoft.mobile.weixin.entity.WxToken;
import com.oceansoft.mobile.weixin.service.WechatService;


/**
 * 商家控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("admin/merchant")
public class SysMerchantController {

    @Resource
    private IMerchantService merchantService;
    @Resource
	private WechatService wechatService;
    
    /**
     * 分页加载问题信息
     *
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "merchantResultList", method = GET)
    public Pager<Merchant> queryPageMerchant(Query query) {
    	Pager<Merchant> p= merchantService.queryPageQuestions(query);
    	return p;
    }
    
    /**
     * 进入新增或编辑商家页面
     *
     * @return String
     */
    @RequestMapping(value = "gotoEditMerchant", method = GET)
    public String gotoEditMerchant(HttpSession session, @RequestParam(value = "sid", required = true) long sid, Model model) {
    	if (sid >0 ) {//编辑
    		Merchant m = merchantService.loadMerchantBySid(sid);
            model.addAttribute("merchant", m);
        } else {//新增
            model.addAttribute("merchant", new Merchant());
        }
        return "admin/merchant-add";
    }
    /**
     * 进入微信对接页面
     *
     * @return String
     */
    @RequestMapping(value = "gotoDeveloperModel", method = GET)
    public String gotoDeveloperModel(HttpSession session, @RequestParam(value = "sid", required = true) String sid, Model model) {
    	if (StringUtils.isNotEmpty(sid)) {//编辑
    		WxToken t = WeixinUtil.loadTokenByMerid(sid);
    		model.addAttribute("token", t);
    	} 
    	return "admin/merchant-token";
    }
    
    /**
     * 表单提交-新增或修改
     *
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "submitMerchant", method = POST)
    public Result saveOrUpdatMerchant(Merchant mer) {
    	mer.setStatus(1);
        return merchantService.saveOrUpdatMerchant(mer);
    }
    

}
